#!/usr/bin/pathon3

import unittest
import json
import time
import re

# from sourcecounter import gitee
import gitee
import sourcecounter

class TestSourceCounter(unittest.TestCase):
    """
    Test Source Counter tool
    """
    def setUp(self):
        self.src_counter = sourcecounter.SourceCounter()

    def tearDown(self):
        pass

    def test_get_contributors(self):
        contributors = self.src_counter.get_contributors("libffado", "src-openeuler")
        print(contributors)

    def test_get_branches(self):
        branches = self.src_counter.get_branches("libffado", "src-openeuler")
        print(branches)

    def test_get_reviewers(self):
        reviewers = self.src_counter.get_reviewers("libffado", "src-openeuler")
        print(reviewers)

    def test_get_commits(self):
        commits = self.src_counter.get_commits("libffado", "src-openeuler")
        print(commits)

    def test_get_commits_1(self):
        commits = self.src_counter.get_commits("mysql", "src-openeuler", sha="master", author="maminjie")
        print(commits)

    def test_get_commits_2(self):
        commits = self.src_counter.get_commits("log4j", "src-openeuler", sha="master", author="wang_yue111")
        print(commits)

    def test_get_one_commit(self):
        commits = self.src_counter.get_one_commit("mysql", "master", "src-openeuler")
        print(commits)

    def test_get_one_commit_1(self):
        commit = self.src_counter.get_one_commit("mysql", "master", "src-openeuler") # last one commit
        print(commit)

    def test_get_one_commit_2(self):
        commit = self.src_counter.get_one_commit("mysql", "570192bd22987fe4df428d5414648d9b85f285a8", "src-openeuler")
        print(commit)

    def test_get_pr_list(self):
        prs = self.src_counter.get_pr_list("libffado", "src-openeuler", head="maminjie:master", base="master")
        print(prs)

    def test_get_pr_comments(self):
        prs = self.src_counter.get_pr_comments("libffado", 4, "src-openeuler")
        print(prs)

    def test_get_repos(self):
        repos = self.src_counter.get_repos("src-openeuler")
        print(len(repos))
        print(repos)

    def test_get_repos_all(self):
        page = 1
        per_page = 100
        repos = self.src_counter.get_repos("src-openeuler", page=page, per_page=per_page)
        while len(repos):
            for repo in repos:
                print(repo)
            page += 1
            repos = self.src_counter.get_repos("src-openeuler", page=page, per_page=per_page)

    def test_merge_statistics(self):
        stats = self.src_counter.merge_statistics("maminjie", "libffado", "src-openeuler",
            start_time="2020-10-01", end_time="2020-10-30")
        print(stats)

    def test_merge_statistics_1(self):
        users = ["maminjie", "wang_yue111"]
        repos = ["efl", "libffado", "pytest", "log4j"]

        print("{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}".format("user", "repo", "branch", "state",
            "prNo.", "prType", "prTitle", "prAuthor", "failCnt", "rejectCnt", 
            "addition", "deletion", "total", "reviewCnt", "reviewer", "time"))
        for user in users:
            for repo in repos:
                stats = self.src_counter.merge_statistics(user, repo, "src-openeuler")
                if not stats:
                    continue
                for stat in stats:
                    print("{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t+{}\t-{}\t{}\t{}\t{}\t{}".format(
                        stat["user"],
                        stat["repo"],
                        stat["branch"],
                        stat["state"],
                        stat["prNum"],
                        stat["prType"],
                        stat["prTitle"],
                        stat["prAuthor"],
                        stat["failCnt"],
                        stat["rejectCnt"],
                        stat["addition"],
                        stat["deletion"],
                        stat["total"],
                        stat["reviewCnt"],
                        stat["reviewer"],
                        stat["time"]))

    def test_contribute_statistics(self):
        stat = self.src_counter.contribute_statistics("maminjie", "libffado", "src-openeuler",
            start_time="2020-10-01", end_time="2020-10-30")
        print(stat)

    def test_contribute_statistics_1(self):
        users = ["maminjie", "wang_yue111"]
        repos = ["efl", "libffado", "pytest", "log4j"]

        print("{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}".format(
            "user", "commitCnt", "mergeCnt", "rejectCnt", "failCnt", "addition", "deletion", "total", "reviewCnt"))
        for user in users:
            commitCnt = 0
            mergeCnt = 0
            rejectCnt = 0
            failCnt = 0
            addition = 0
            deletion = 0
            total = 0
            reviewCnt = 0
            for repo in repos:
                stat = self.src_counter.contribute_statistics(user, repo, "src-openeuler")
                if not stat:
                    continue
                commitCnt += stat["commitCnt"]
                mergeCnt += stat["mergeCnt"]
                rejectCnt += stat["rejectCnt"]
                failCnt += stat["failCnt"]
                addition += stat["addition"]
                deletion += stat["deletion"]
                total += stat["total"]
                reviewCnt += stat["reviewCnt"]
            print("{}\t{}\t{}\t{}\t{}\t+{}\t-{}\t{}\t{}".format(user, commitCnt, mergeCnt, rejectCnt,
                failCnt, addition, deletion, total, reviewCnt))

    def test_time_compare(self):
        s1 = "2020-09-19T09:10:37+08:00"
        s2 = "2020-09-20T09:10:37+08:00"
        if s1 < s2:
            print("yes")

if __name__ == "__main__":
    unittest.main()
